package com.tbit.uqbike.client.controller;

import com.tbit.uqbike.client.constant.RedisConstant;
import com.tbit.uqbike.client.constant.ResponseCode;
import com.tbit.uqbike.client.exception.BaseException;
import com.tbit.uqbike.client.pojo.Machine;
import com.tbit.uqbike.client.service.MachineService;
import com.tbit.uqbike.client.service.RedisService;
import com.tbit.uqbike.client.util.Assert;
import com.tbit.uqbike.client.util.OutputUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 手机道钉信号上传
 *
 * @Author: chen
 * @Description: 手机道钉信号上传
 * @Date: 创建于 10:41 2021/1/6
 */
@Controller
@RequestMapping("/ble")
public class BleController {

    @Autowired
    private MachineService machineService;
    @Autowired
    private RedisService redisService;


    /**
     * 上传
     *
     * @param response 响应
     * @ignoreParams request
     */
    @RequestMapping("/upload")
    public void upload(String userCode, String bleNOs, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();

        try {
            /* 校验 */
            Assert.notNull(userCode, "-30006", "设备编号不能为空");
            Assert.notNull(bleNOs, "-160009", "道钉信息不能为空");
            Machine machine = machineService.getByUserCode(userCode);
            if (machine == null) {
                throw new BaseException("-30005", "设备不存在");
            }
            //LogUtil.info("设备"+userCode+"收到手机道钉信号"+bleNOs);
            redisService.add(RedisConstant.PHONE_UPLOAD_BLE + userCode, bleNOs, 60);

            map.put(ResponseCode.RET, ResponseCode.SUCC);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }
        OutputUtil.renderText(request, response, map);
    }

}
